package cn.com.Servlet.Comment;

import cn.com.controler.Comment.CommentDAO;
import cn.com.entity.Comment;
import cn.com.entity.CommentView;
import cn.com.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "CommentServlet",value = "/CommentDML")
public class CommentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String type = request.getParameter("type");
        HttpSession session = request.getSession();
        User use = (User)session.getAttribute("user");
        Integer userId = use.getUserId();
        CommentDAO commentDAO = new CommentDAO();
        if(type.equals("add")){      //帖子下面添加评论
            String why = request.getParameter("why");
            Integer artid = Integer.valueOf(request.getParameter("artid"));
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String fbtime = format.format(date);
            Comment comment = new Comment(1,userId,artid,why,fbtime);
            int i = commentDAO.addComment(comment);
            response.getWriter().write("1");
            }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String czlx = request.getParameter("czlx");
        HttpSession session = request.getSession();
        CommentDAO commentDAO = new CommentDAO();
        CommentView commentView = new CommentView();
        if(czlx.equals("cx")){       //查询我的评论
            //         获取用户id
            User use = (User)session.getAttribute("user");
            Integer userId = use.getUserId();
            //获取评论
            List userCommentlist = commentDAO.selectcommentbyuserid(userId);
            session.setAttribute("usercomment",userCommentlist);
            request.getRequestDispatcher("/WebContent/HTML/UserPage/user_comment/MyContent.jsp").forward(request,response);
        }else if(czlx.equals("delete")){      //删除我的评论
             Integer id = Integer.valueOf(request.getParameter("commentid"));
            int deletebyid = commentDAO.deletebyid(id);
            response.sendRedirect("/CommentDML?czlx=svpl");
        }else if(czlx.equals("svpl")){
            //         获取用户id
            User use = (User)session.getAttribute("user");
            Integer userId = use.getUserId();
            //获取评论
            List userCommentlist = commentDAO.selectcommentbyuserid(userId);
            session.setAttribute("usercomment",userCommentlist);
            response.sendRedirect("/WebContent/HTML/UserPage/user_comment/DeleteComment.jsp");
        }

    }
}
